package offer.tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Stack;

/**
 * @author DengYuan2
 * @create 2021-03-17 19:49
 */
public class NC14 {
    public static void main(String[] args) {
        TreeNode t1 = new TreeNode(3);
        TreeNode t2 = new TreeNode(9);
        TreeNode t3 = new TreeNode(20);
        TreeNode t4 = new TreeNode(15);
        TreeNode t5 = new TreeNode(7);
        t1.left=t2;
        t1.right=t3;
        t2.left=t4;
        t2.right=t5;
        NC14 n = new NC14();
        ArrayList<ArrayList<Integer>> res = n.zigzagLevelOrder2(t1);
        System.out.println(res);
    }

    /**
     * 一个队列
     * @param root
     * @return
     */
    public ArrayList<ArrayList<Integer>> zigzagLevelOrder (TreeNode root) {
        ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
        LinkedList<TreeNode> nodes = new LinkedList<>();
        if (root==null){
            return ret;
        }
        nodes.add(root);
//        int layer=0;
        //直接用flag来辨别奇偶
        boolean flag =true;
        while (!nodes.isEmpty()){
            ArrayList<Integer> list = new ArrayList<>();
            int size=nodes.size();
//            layer++;
            for (int i = 0; i < size; i++) {
                TreeNode parent = nodes.poll();
                //layer也可以直接用(ret.size()+1)哦
//                if (layer%2==0){
//                    //偶数层，头插
//                    list.add(0,parent.val);
//                }else {
//                    list.add(parent.val);
//                }
                if (flag){
                    list.add(parent.val);
                }else {
                    list.add(0,parent.val);
                }
                if (parent.left!=null){
                    nodes.add(parent.left);
                }
                if (parent.right!=null){
                    nodes.add(parent.right);
                }
            }
            flag=!flag;
            ret.add(list);
        }
        return ret;
    }

    /**
     * 两个栈，一个奇，一个偶
     * @param root
     * @return
     */
    public ArrayList<ArrayList<Integer>> zigzagLevelOrder2 (TreeNode root){
        ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
        if (root==null){
            return ret;
        }
        Stack<TreeNode> oddStack = new Stack<>();
        Stack<TreeNode> evenStack = new Stack<>();
        oddStack.push(root);
        while (!oddStack.isEmpty() || !evenStack.isEmpty()){
            ArrayList<Integer> list = new ArrayList<>();
            while (!oddStack.isEmpty()){
                TreeNode pop = oddStack.pop();
                list.add(pop.val);
                if (pop.left!=null){
                    evenStack.push(pop.left);
                }
                if (pop.right!=null){
                    evenStack.push(pop.right);
                }

            }
            if (!list.isEmpty()){
                ret.add(list);
            }
            ArrayList<Integer> list2 = new ArrayList<>();
            while (!evenStack.isEmpty()){
                TreeNode pop = evenStack.pop();
                list2.add(pop.val);
                if (pop.right!=null){
                    oddStack.push(pop.right);
                }
                if (pop.left!=null){
                    oddStack.push(pop.left);
                }
            }
            if (!list2.isEmpty()){
                ret.add(list2);
            }
        }
        return ret;
    }
}
